﻿using UnityEngine;
using System.Collections;

namespace AutoRoot
{
   [RequireComponent(typeof(BoxCollider))]
   public class Progressbar : MonoBehaviour
   {
      public Transform knob;
      public TextMesh currentTimeLabel;
      public TextMesh totalTimeLabel;
      private float totalTime = 1;
      private float currentTime = 0;
      private Vector3 targetPos;
      private float sliderLength;
      private float leftEdgeX;
      private float rightEdgeX;

      private bool initialized = false;

      private bool isPlaying = false;

      void OnReInit()
      {
         initialized = false;
      }

      void Start()
      {
         GetEdgesAndLength();
         knob.position = (new Vector3(leftEdgeX, knob.position.y, knob.position.z));
         targetPos = knob.position;
      }

      void GetEdgesAndLength()
      {
         sliderLength = GetComponent<BoxCollider>().renderer.bounds.size.x;
         leftEdgeX = 0 - (sliderLength / 2);
         rightEdgeX = (sliderLength / 2);
      }

      public void Init()
      {
         GetEdgesAndLength();

         renderer.material.SetFloat("_Progress", scrubberPosition() / sliderLength);

         initialized = true;

         currentTimeLabel.text = Seconds2Minutes(currentTime);
         totalTimeLabel.text = Seconds2Minutes(totalTime);
      }

      void Update()
      {
         UpdateKnobPosition(new Vector3(targetPos.x, knob.position.y, knob.position.z));
         if (!initialized)
         {
            Init();
         }
      }

      void OnTouchDown(Vector3 point)
      {
         isPlaying = Global.currentAudio.isPlaying;
         UpdateKnobPosition(point);
      }

      void OnTouchStay(Vector3 point)
      {
         if (isPlaying)
         {
            Global.currentAudio.Pause();
         }
         UpdateKnobPosition(point);
         Global.currentAudio.time = currentTime;
      }

      void OnScreenTouchUp(Vector3 hitPoint)
      {
         ToPreviousStatus();
      }

      void OnScreenTouchExit(Vector3 hitPoint)
      {
         ToPreviousStatus();
      }

      void OnTouchUp(Vector3 point)
      {
         ToPreviousStatus();
      }

      void OnTouchExit(Vector3 point)
      {
         ToPreviousStatus();
      }

      void ToPreviousStatus()
      {
         if (isPlaying)
         {
            Global.currentAudio.time = currentTime;
            Global.currentAudio.Play();
         }
      }

      void UpdateKnobPosition(Vector3 point)
      {
         targetPos.x = point.x;
         if (targetPos.x <= leftEdgeX)
         {
            targetPos.x = leftEdgeX;
         }
         else if (targetPos.x >= rightEdgeX)
         {
            targetPos.x = rightEdgeX;
            Global.currentAudio.time = 0;

            if (GameObject.Find("ControlPart").GetComponent<ControlPart>().playingSection)
            {
               Global.LoadAudio(Global.currentSection.Value.decisionResourcePath);
               GameObject.Find("ControlPart").SendMessage("OnPlayDecision", 3, SendMessageOptions.DontRequireReceiver);

               updateTotalTime(Global.currentAudio.clip.length);
               updateByTime(0);

               GameObject.Find("ButtonReplay").GetComponent<Replay>().Execute();
            }
         }
         knob.position = (new Vector3(targetPos.x, knob.position.y, knob.position.z));
         updateByTime((targetPos.x - leftEdgeX) / sliderLength * totalTime);

         renderer.material.SetFloat("_Progress", scrubberPosition() / sliderLength);
      }

      float scrubberPosition()
      {
         return knob.position.x - leftEdgeX;
      }

      string Seconds2Minutes(float second)
      {
         float minutes = Mathf.Floor(second / 60);
         int seconds = Mathf.RoundToInt(second % 60);
         if (seconds == 60)
         {
            ++minutes;
            seconds = 0;
         }
         return minutes.ToString("F0") + ":" + seconds.ToString("D2");
      }

      void updateTotalTime(float time)
      {
         if (time > 0)
         {
            totalTime = time;
            updateByTime(currentTime);
         }
      }

      void updateByTime(float time)
      {
         if (totalTime >= time)
         {
            currentTime = time;
            currentTimeLabel.text = Seconds2Minutes(currentTime);
            targetPos.x = (time * sliderLength) / totalTime + leftEdgeX;
         }
      }
   }
}